package cn.th.controller;


import cn.th.common.R;
import cn.th.entity.School;
import cn.th.entity.Task;
import cn.th.service.ISchoolService;
import cn.th.service.ITaskService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;

/**
 * <p>
 * 任务表 前端控制器
 * </p>
 *
 * @author cn.th
 * @since 2024-03-30
 */
@Slf4j
@RestController
@RequestMapping("/task")
public class TaskController {

    @Autowired
    private ITaskService taskService;

    /**
     * 查询任务表
     * @param page
     * @param pageSize
     * @param school 学校名
     * @param publishName 发布学院名字
     * @param status 状态
     * @param points 积分
     * @param name 任务名字
     * @return
     */
    @GetMapping("/list")
    public R<Map<String, Object>> page(@RequestParam(value = "page") int page,
                                       @RequestParam(value = "limit") int pageSize,
                                       @RequestParam(value = "school", required = false) String school,
                                       @RequestParam(value = "publishName", required = false) String publishName,
                                       @RequestParam(value = "status", required = false) String status,
                                       @RequestParam(value = "points", required = false) String points,
                                       @RequestParam(value = "name", required = false) String name,
                                       @RequestParam(value = "importance", required = false) String importance){

        Map<String, Object> data = taskService.queryTask(page,pageSize,school,publishName,status,points,name,importance);
        return R.success(data);
    }


    /**
     * 新增任务
     * @param task
     * @return
     */
    @PostMapping()
    public R<String> save(@RequestBody Task task) {
        taskService.save(task);
        return R.success("新增任务成功！！！");
    }

    /**
     * 删除任务，可批量删除
     * @param ids
     */
    @DeleteMapping
    public R<String> delete(String ids){
        taskService.removeBatchByIds(ids);
        return R.success("删除任务成功");
    }

    /**
     * 修改任务
     * @param task
     * @return
     */
    @PutMapping
    public R<String> update(@RequestBody Task task){

        log.info(task.toString());

        taskService.updateById(task);
        return R.success("修改学校成功");
    }

    /**
     * 修改任务时页面的数据展示
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<Task> getTaskById(@PathVariable("id")Long id){
        Task task = taskService.getById(id);
        return R.success(task);
    }
}

